//
//  CycleView.m
//  LearnShare
//
//  Created by foreveross－bj on 14/12/18.
//  Copyright (c) 2014年 foreveross－bj. All rights reserved.
//

#import "CycleView.h"

@interface CycleView ()
{
    CGFloat lineLong; //视图的宽度
}

@property (nonatomic, strong) UIView * hiddenView; //遮罩层

@end
@implementation CycleView

#pragma mark - init 

- (id)initWithFrame:(CGRect)frame hiddenColor:(UIColor *)hiddenColor progressColor:(UIColor *)progressColor{
    self = [super initWithFrame:frame];
    if (self) {
        lineLong = MIN(frame.size.height, frame.size.width);
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, lineLong, lineLong);
        self.layer.cornerRadius = lineLong / 2;
        self.layer.masksToBounds = YES;
        self.hiddenView = [[UIView alloc] initWithFrame:self.bounds];
        _hiddenView.backgroundColor = hiddenColor;
        [self addSubview:_hiddenView];
        self.backgroundColor = progressColor;
    }
    return self;
}

#pragma mark - setting and getting

/**
 * 设置进度
 * @param progress
 **/
- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self setHiddenVIewBoundsWithProgress:progress];
}
/**
* 获取进度
* @param CGFloat
**/
- (CGFloat)progress{
    return _progress;
}
/**
 * 设置遮罩层颜色
 * @param hiddenColor
 **/
- (void)setHiddenColor:(UIColor *)hiddenColor{
    _hiddenColor = hiddenColor;
    _hiddenView.backgroundColor = _hiddenColor;
}
/**
 * 获取遮罩层颜色
 * @param UIColor
 **/
- (UIColor *)hiddenColor{
    return _hiddenView.backgroundColor;
}

#pragma mark - private function
/**
 * 改变遮罩层的frame
 * @param progress
 **/
- (void)setHiddenVIewBoundsWithProgress:(CGFloat)progress{
    CGRect bounds = self.bounds;
    bounds.size.height = lineLong - progress * lineLong;
    [UIView animateWithDuration:2 animations:^{
        self.hiddenView.frame = bounds;
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
